package com.lcxw.hxzbadmin.controller.richlevelcontroller;

import com.alibaba.fastjson.JSONObject;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbapi.entity.pojo.RichLevel;
import com.lcxw.hxzbadmin.entity.requestbean.ModRichLevelRequestBean;
import com.lcxw.hxzbapi.service.RichLevelService;
import com.lcxw.hxzbapi.entity.response.Response;

@RestController
@CrossOrigin
@RequestMapping("admin/richLevel")
public class RichLevelController {

    private Logger logger = LoggerFactory.getLogger(RichLevelController.class);

    @Resource
    RichLevelService richLevelService;

    @PostMapping("/getAll")
    public Response getAllRichLevel(@RequestBody JSONObject requestParams) {
        logger.info("admin {} get all rich level", requestParams.getString("adminName"));

        Map<String, Object> data = new LinkedHashMap<>();
        List<RichLevel> richLevels;

        try {
            richLevels = richLevelService.selectAll();
        } catch (Exception e) {
            return Response.dbFail(logger, "get all rich level");
        }
        data.put("richLevels", richLevels);
        return Response.common(logger, "get all rich level success", 0, "success", data);
    }

    @PostMapping("/mod")
    public Response modRichLevelList(@RequestBody ModRichLevelRequestBean modRichLevelRequestBean) {
        logger.info("admin {} mod rich level list ", modRichLevelRequestBean.getAdminName());

        List<RichLevel> richLevelList = modRichLevelRequestBean.getRichLevels();

        try {
            for (RichLevel richLevel : richLevelList
            ) {
                if (null == richLevel.getId() || null == richLevelService
                    .selectByPrimaryKey(richLevel.getId())) {
                    richLevelService.insert(richLevel);
                } else {
                    richLevelService.updateByPrimaryKey(richLevel);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "operate rich level");
        }
        return Response.common(logger, "mod rich level success.", 0, "success", null);

    }

    @PostMapping("/add")
    public Response addRichLevel() {
        return null;
    }

    @PostMapping("/delete")
    public Response deleteRichLevel(@RequestBody JSONObject requestParams) {
        logger.info("admin {} delete rich level", requestParams.getString("adminName"));

        int id = requestParams.getInteger("id");

        try {
            richLevelService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "delete rich level");
        }

        return Response.common(logger, "delete rich level success.", 0, "success", null);

    }

}
